<?php
namespace Admin\Form;
use Common\Form\BaseForm;
use Common\Model\CurrencyModel;
use Common\Model\UserMoneyModel;
use Common\Model\FinanceLogModel;
class UserMoneyForm extends BaseForm {
    public static function newSelf()
    {
        return new self();
    }
    protected $data;
    protected $action;
    protected $return_type;
    public function __construct()
    {
        set_time_limit(0);
        parent::__construct();
        $this->return_type='ajax';
        $this->data['changeUserMoney']=[
            'user_id'=>'user_id',
            'type'=>'type',
            'num'=>'num',
            'currency_id'=>'currency_id'
        ];
    }
    public function changeUserMoney($input)
    {
        $this->action='changeUserMoney';
        $this->__toInitialize($input);
        $this->emptyVerification('user_id','用户不存在');
        $this->emptyVerification('type','请选择类型');
        $this->emptyVerification('num','请填写金额');
        $this->emptyVerification('currency_id','请选择币种');
        
        
        $this->findUserByUserId('user_id', '用户不存在');
        
        $currency=CurrencyModel::newSelf()->findOneById($this->getDataVal('currency_id'));
        $this->emptyVerificationManual($currency,'币种不存在');
        
        $model=UserMoneyModel::newSelf();
        if ($this->getDataVal('type')=='inc') {
            $res=$model->incUserMoney($this->getDataVal('user_id'), $this->getDataVal('currency_id'),  $this->getDataVal('num'));
            FinanceLogModel::newSelf()->addLog(101,$this->getDataVal('num'),$this->getDataVal('user_id'),$this->getDataVal('currency_id'));
        }
        if ($this->getDataVal('type')=='dec') {
            $this->moneyPayVerification($this->getDataVal('currency_id'),'该用户金额不足'.$this->getDataVal('num'));
            $res=$model->decUserMoney($this->getDataVal('user_id'), $this->getDataVal('currency_id'),  $this->getDataVal('num'));
            FinanceLogModel::newSelf()->addLog(102,$this->getDataVal('num'),$this->getDataVal('user_id'),$this->getDataVal('currency_id'));
        }
        $this->resReturn($res, '操作成功', '操作失败');
    }
    
   
    
}